//
//  FillDetailDataViewController.m
//  CommunityNannyO2O
//
//  Created by 90 on 15/7/21.
//  Copyright (c) 2015年 Apple. All rights reserved.
//

#import "FillDetailDataViewController.h"
#import "AppConfig.h"
#import "QXNetWorkTool.h"
#import "APIAdress.h"
#import "MBProgressHUD+YC.h"
#import "MJExtension.h"
#import "AccountTool.h"
#import "FillDetailDataTableViewCell.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import "UIActionSheet+Blocks.h"

@interface FillDetailDataViewController()<QXNetWorkToolDelegate,
        UITableViewDataSource,UITableViewDelegate,
        UIImagePickerControllerDelegate,UINavigationControllerDelegate,FillPhotoDeletage>
{

}

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property UIImagePickerController *headPicker;

@end

@implementation FillDetailDataViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self configNavigationItem];
    
    self.tableView.dataSource = self;
    
    self.tableView.delegate = self;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 586;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FillDetailDataTableViewCell *cell = [FillDetailDataTableViewCell cellWithTableView:tableView];
    cell.fatherViewController = self;
    cell.delegate = self;
    
    return cell;
}

-(void)configNavigationItem
{
    self.navigationItem.title = @"填写详细资料";
    [self.navigationController.navigationBar setTranslucent:NO];
    
    [self.navigationController.navigationBar
     setBackgroundImage:[UIImage imageNamed:@"bg_navigation_bar"]
     forBarMetrics:UIBarMetricsDefault];
    
    //status bar的字体为白色
    //导航栏的背景色是黑色
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    //设置导航条title
    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
    shadow.shadowOffset = CGSizeMake(0, 0);
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                 [UIColor whiteColor],NSForegroundColorAttributeName,
                 shadow,NSShadowAttributeName,
                 [UIFont fontWithName:@"Hiragino Sans GB" size:18.0],NSFontAttributeName,
                 nil]];
    
    [self configNavigationBackAction:^{
        
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    
    [self configNavigationRightString:@"跳过"
                             textFont:[UIFont fontWithName:@"Hiragino Sans GB" size:14]
                            textColor:UIColorRGBA(151, 151, 151, 1)
                            andAction:^{
                                [self pressRight];}];
}

-(void)pressRight
{
    [self.navigationController popToRootViewControllerAnimated:NO];
}

-(void)finish
{
    [self.navigationController popToRootViewControllerAnimated:NO];
}

-(void)useMethodToFindBlackLineAndHind
{
    UIImageView* blackLineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    
    blackLineImageView.hidden = YES;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self configNavigationItem];

    [self useMethodToFindBlackLineAndHind];
}

-(void)setHead:(UIImage *)image
{
    FillDetailDataTableViewCell *cell =
    (FillDetailDataTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    
    [cell setHeadPhoto:image];
    cell.Photo.hidden = YES;
}

- (void)loadUpHeadphoto
{
    self.headPicker = [[UIImagePickerController alloc] init];
    self.headPicker.mediaTypes = @[(NSString *)kUTTypeImage];
    self.headPicker.allowsEditing = YES;
    self.headPicker.delegate = self;
    
    if (YES/*[UIImagePickerController isSourceTypeAvailable:
    UIImagePickerControllerSourceTypeCamera]*/) {
        WEAKSELF
        [UIActionSheet showInView:self.view withTitle:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@[@"拍照", @"从相册选择"] tapBlock:^(UIActionSheet *actionSheet, NSInteger buttonIndex) {
            switch (buttonIndex) {
                case 0:
                    weakSelf.headPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                    break;
                case 1:
                    weakSelf.headPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    break;
                default:
                    break;
            }
            if (buttonIndex != actionSheet.cancelButtonIndex) {
                [weakSelf.navigationController presentViewController:weakSelf.headPicker animated:YES completion:nil];
            }
        }];
    }
    else
    {
        self.headPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self.navigationController presentViewController:self.headPicker animated:YES completion:nil];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    if (picker == self.headPicker)
    {
        [picker dismissViewControllerAnimated:YES completion:^{
            
            UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
            
            [self setHead:editedImage];
        }];
    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

@end




























